package com.splant.android.smart.UILevel.Community.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.Global;
import com.splant.android.smart.ModelLevel.GatewayListBean;
import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/21.
 */

public class Garden_GatewayList_Fragment extends Fragment{

    private static final int REQUEST_COUNT = 3;

    private sPlantAsyncHttpClient sPlantClient;
    private View mView;
    private LRecyclerView lrv_gatewayList;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private Garden_GatewayList_Adapter garden_gatewayList_adapter;
    private List<GatewayListBean.GateWay> gateWayList = new ArrayList<>();


    private boolean isLoadFinish;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {

            mView = inflater.inflate(R.layout.garden_gatewaylist_fragment,null);

            lrv_gatewayList= (LRecyclerView) mView.findViewById(R.id.lrv_gatewayList);
            sPlantClient = sPlantAsyncHttpClient.getInstance();

            init();
        }
        if (isLoadFinish==false)
            getDatas();

        return mView;
    }
    private void init()
    {
        garden_gatewayList_adapter = new Garden_GatewayList_Adapter(getActivity());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(garden_gatewayList_adapter);

        lrv_gatewayList.setHasFixedSize(true);
        lrv_gatewayList.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        lrv_gatewayList.setAdapter(lRecyclerViewAdapter);
        lrv_gatewayList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        lrv_gatewayList.setOnRefreshListener(onRefreshListener);

        lrv_gatewayList.setLoadMoreEnabled(false);

    }

    public void getDatas() {

        RequestParams params = new RequestParams();
        params.put("userId", Global.user.getUserId());

        sPlantClient.get(sPlantAsyncHttpClient.GETGATEWAYS, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try
                {
                    if (response.getInt("succeed") == 1)
                    {
                        isLoadFinish=true;

                        Gson gson = new Gson();

                        GatewayListBean gatewayListBean = gson.fromJson(response.toString(), GatewayListBean.class);

                        if (gatewayListBean.getGateWayList() != null&&gatewayListBean.getGateWayList().size()>0) {

                            gateWayList.addAll(gatewayListBean.getGateWayList());

                            garden_gatewayList_adapter.setDataList(gateWayList);
                        }

                        lrv_gatewayList.refreshComplete(REQUEST_COUNT);
                    }
                }
                catch (Exception e)
                {
                    lrv_gatewayList.refreshComplete(REQUEST_COUNT);
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                lrv_gatewayList.refreshComplete(REQUEST_COUNT);
                Toast.makeText(getActivity(), getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();
            }
        });

    }

    private OnRefreshListener onRefreshListener=new OnRefreshListener() {
        @Override
        public void onRefresh() {
            gateWayList.clear();
            getDatas();
        }
    };


}
